package com.wzy.common.base.sms;

import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;

/**
 * 2 * @Author: wuzhengyu
 * 3 * @Date: 2021/3/14 10:23
 * 4
 */
public class AliyunSmsSend {

    public static void main(String[] args) {
        System.out.println(sendMessage("15003573003", "123456"));

    }

    /**
     * 可以考虑将里面的参数都抽象出来
     * @param phone
     * @param code
     * @return
     */
    public static boolean sendMessage(String phone, String code) {
        // 用户登录名称 wuzhengyu@1149668726917740.onaliyun.com
        //AccessKey ID LTAI4G6JKtFMrAae4orfYiYX
        //AccessKey Secret 2fN9IepWYtZRtAaBdE2gLUFH9eM2GO
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "LTAI4G6JKtFMrAae4orfYiYX", "2fN9IepWYtZRtAaBdE2gLUFH9eM2GO");
        IAcsClient client = new DefaultAcsClient(profile);
        CommonRequest request = new CommonRequest();
        request.setSysMethod(MethodType.POST);
        request.setSysDomain("dysmsapi.aliyuncs.com");
        request.setSysVersion("2017-05-25");
        request.setSysAction("SendSms");
        request.putQueryParameter("RegionId", "cn-hangzhou");
        request.putQueryParameter("PhoneNumbers", phone);
        request.putQueryParameter("SignName", "唐红豆论坛");
        request.putQueryParameter("TemplateCode", "SMS_192540207");
        request.putQueryParameter("TemplateParam", "{\"code\":\""+code +"\"}");
        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());
            String[] str = response.getData().split(",");
            for (String s : str) {
                System.out.println(s);
            }
            return str[1].equals("\"Message\":\"OK\"");
        } catch (ClientException e) {
            e.printStackTrace();
            return false;
        }
    }

    public static boolean sendMessage(String name, String nextTime, String phone) {
        // 用户登录名称 wuzhengyu@1149668726917740.onaliyun.com
        //AccessKey ID LTAI4G6JKtFMrAae4orfYiYX
        //AccessKey Secret 2fN9IepWYtZRtAaBdE2gLUFH9eM2GO
        StringBuilder sb = new StringBuilder();
        for (int i=0; i<nextTime.length(); i++) {
            if(nextTime.charAt(i) == ' ') {
                sb.append("o");
            } else {
                sb.append(nextTime.charAt(i));
            }
        }
        nextTime = sb.toString();
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "LTAI4G6JKtFMrAae4orfYiYX", "2fN9IepWYtZRtAaBdE2gLUFH9eM2GO");
        IAcsClient client = new DefaultAcsClient(profile);
        CommonRequest request = new CommonRequest();
        request.setSysMethod(MethodType.POST);
        request.setSysDomain("dysmsapi.aliyuncs.com");
        request.setSysVersion("2017-05-25");
        request.setSysAction("SendSms");
        request.putQueryParameter("RegionId", "cn-hangzhou");
        request.putQueryParameter("PhoneNumbers", phone);
        request.putQueryParameter("SignName", "唐红豆论坛");
        request.putQueryParameter("TemplateCode", "SMS_192540207");
        request.putQueryParameter("TemplateParam", "{\"code\":\"" + nextTime+ "\"}");
        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());
            String[] str = response.getData().split(",");
            for (String s : str) {
                System.out.println(s);
            }
            return str[1].equals("\"Message\":\"OK\"");
        } catch (ClientException e) {
            e.printStackTrace();
            return false;
        }
    }
}
